package org.example.redis1.task;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Set;

@Component
public class NewsJob {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private RedisTemplate redisTemplate;

    @Scheduled(cron = "0/10 * * * * ?")
    public void SaveNewsData(){
        Set<String> keys = redisTemplate.keys("news_*");
        for(String key:keys){
            Map<String,String> map = redisTemplate.opsForHash().entries(key);
            String id = key.substring(5);
            String read = map.get("read");
            String zan = map.get("zan");
            logger.info("编号："+id+" 阅读量："+read+" 点赞量："+zan);
        }
        logger.info("保存新闻数据完成");
    }
}
